package cn.huanba.controller;

import cn.huanba.client.UserService;
import cn.huanba.dto.Result;
import cn.huanba.entity.User;
import cn.huanba.utils.RedisUtils;
import cn.huanba.utils.ResultUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-01-07 17:23:28
 */
@RestController
@RequestMapping("user")
@Api(value = "用户控制类", tags = "用户控制类", description = "用户模块控制类描述")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("/login")
    @ApiOperation(value = "手机登录", notes = "手机+验证码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query", required = true, dataType = "string"),
            @ApiImplicitParam(name = "sms", value = "验证码", paramType = "query", required = true, dataType = "string")
    })
    public Result loginBySms(String phone, String sms)throws Exception{
        User user=null;
        //判断验证码
        String redisCode=redisUtils.get(phone);
        if(redisCode==null || !sms.equals(redisCode)){
            return ResultUtils.returnFail("验证码错误！");
        }

        user=userService.loginByPhone(phone);
        //判断账号是否存在
        if(user==null){
            return ResultUtils.returnFail("账号不存在！");
        }
        String token=createToken(user,"phonelogin");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("token",token);
        return ResultUtils.returnDataSuccess(map);
    }

    @RequestMapping("/loginByLoginname")
    @ApiOperation(value = "账号密码登录", notes = "账号密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginname", value = "用户名", paramType = "query", required = true, dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query", required = true, dataType = "string")
    })
    public Result loginByLoginname(String loginname, String password)throws Exception{
        User user=null;

        user=userService.loginByLoginname(loginname,password);
        //判断账号是否存在
        if(user==null){
            return ResultUtils.returnFail("账号或者密码错误！");
        }
        String token=createToken(user,"password");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("token",token);
        return ResultUtils.returnDataSuccess(map);
    }



    //创建token，并保存至 redis
    public String createToken(User user,String type)throws Exception{
        String token= UUID.randomUUID().toString();
        token="huanba:"+type+":"+token;
        //保存token
        redisUtils.set(token, JSONObject.toJSONString(user),30*60);//30分钟
        return token;
    }




}